package com.acme.bankapp.server;

import com.acme.bankapp.domain.Bank;
import com.acme.bankapp.domain.Client;
import com.acme.bankapp.domain.Gender;
import com.acme.bankapp.domain.accounts.Account;
import com.acme.bankapp.domain.accounts.CheckingAccount;
import com.acme.bankapp.domain.accounts.SavingAccount;
import com.acme.bankapp.service.BankService;

import java.io.IOException;

/**
 * Created by Java Student on 11/19/2014.
 */
public class BankServerService {

    public static void info(Bank bank, BankServerTransport bankServerTransport) throws IOException {
        for (Client client : bank.getClients()) {
            bankServerTransport.writeLine(client.toString());
        }
        bankServerTransport.writeLine("eof");
    }

    public static void incorrectCommand(BankServerTransport bankServerTransport) throws IOException {
        bankServerTransport.writeLine("Incorrect command!");
        bankServerTransport.writeLine("eof");
    }

    public static void exit(BankServerTransport bankServerTransport) throws IOException {
        bankServerTransport.writeLine("eof");
    }

    public static void addNewClient(Bank bank, BankServerTransport bankServerTransport) throws IOException {
        String name = getStringFromClient(bankServerTransport, "Enter client's name:");
        String genType = getStringFromClient(bankServerTransport, "Enter client's gender (m/f):");
        String accType = getStringFromClient(bankServerTransport, "Enter account type (c/s):");
        String balanceString = getStringFromClient(bankServerTransport, "Enter start balance:");
        double balance = Double.parseDouble(balanceString);

        Gender gender = switchGenType(genType);
        Account account = switchAccType(bankServerTransport, accType, balance);
        Client client = new Client(name, gender, account);

        BankService.addClient(bank, client);

        //TODO: fix hardcoded file
        BankService.saveBank(bank, "bank.ser");
        bankServerTransport.writeLine("Client added.");
        bankServerTransport.writeLine("eof");
    }

    private static String getStringFromClient(BankServerTransport bankServerTransport, String message) throws IOException {
        bankServerTransport.writeLine(message);
        bankServerTransport.writeLine("eof");
        return bankServerTransport.readLine();
    }

    private static Account switchAccType(BankServerTransport bankServerTransport, String accType, double balance) throws IOException {
        switch (accType) {
            case "s":
                return new SavingAccount(balance);
            case "c":
                bankServerTransport.writeLine("Enter overdraft:");
                bankServerTransport.writeLine("eof");
                double overdraft = Double.parseDouble(bankServerTransport.readLine());
                return new CheckingAccount(balance, overdraft);
            default:
                throw new RuntimeException("Unsupported account type");
        }
    }

    private static Gender switchGenType(String g) {
        Gender gender;
        switch (g) {
            case "m":
                gender = Gender.MALE;
                break;
            case "f":
                gender = Gender.FEMALE;
                break;
            default:
                throw new RuntimeException("Unsupported gender type");
        }
        return gender;
    }
}
